package com.aabte.design2.creational.singleton;

public class LazyDoubleCheckSingleton {

    /**
     * 使用volatile关键字禁止重排序
     */
    private volatile static LazyDoubleCheckSingleton instance = null;

    private LazyDoubleCheckSingleton() {
    }

    public static LazyDoubleCheckSingleton getInstance() {
        if (null == instance) {
            synchronized (LazyDoubleCheckSingleton.class) {
                if (null == instance) {
                    /*
                    1. 给对象分配内存
                    2. 初始化对象
                    3. 设置instance指向刚才分配的内存
                    在单线程内，2和3步可能执行顺序不固定（提高性能）
                     */
                    instance = new LazyDoubleCheckSingleton();
                }
            }
        }
        return instance;
    }

}
